package com.prj.dy.datamgr.undo
{
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;	
	import mx.core.UIComponent;
	
	import com.prj.dy.datamgr.undo.CommandBase;

	//======================命令管理：Undo，Redo=============================	
	public class CommandManager extends EventDispatcher		
	{
		
		private static var g_Created:Boolean = false;		
		public static var Instance:CommandManager = new CommandManager();


		private var _undoList:ArrayCollection = new ArrayCollection();		
		private var _redoList:ArrayCollection = new ArrayCollection();
		
		
		public function CommandManager()			
		{			
			super();
		}
		
		public function get canRedo():Boolean		
		{			
			return _redoList.length > 0;			
		}
		
		public function get canUndo():Boolean			
		{			
			return _undoList.length > 0;			
		}
		
		public function redo():void		
		{			
			if (!canRedo)				
				return;
			
			var command:CommandBase = _redoList.removeItemAt(_redoList.length - 1) as CommandBase;
			
			command.execute();
			
			_undoList.addItem(command);			
		}
		
		public function undo():void			
		{			
			if (!canUndo)				
				return;
			
			var command:CommandBase = _undoList.removeItemAt(_undoList.length - 1) as CommandBase;
			
			command.unExecute();
			
			_redoList.addItem(command);			
		}
		
		//======================命令调用=============================		
		public function executeCommand(command:CommandBase):void			
		{			
			command.execute();			
			appendCommand(command);			
		}
		
		private function appendCommand(command:CommandBase):void			
		{			
			//有新命令添加时清空RedoList，			
			_undoList.addItem(command);			
		}		
		
	} 
}